#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/if_ether.h>
#include <fcntl.h>
#include <iostream>
#include <iomanip>
#include "protocol.h"
#define INTERFACE "eth0"          /* 默认网卡 */


using namespace std;
class rawsocket
{
    private:
	int sockfd;
    public:
	rawsocket(const int protocol);
	// ~rawsocket() ;
	//set the promiscuous mode.
	bool dopromisc(char *nif);
	//capture packets.
	int receive(char *recvbuf,int buflen,struct sockaddr_in *from,int *addrlen);
};


typedef struct filter
{
    unsigned long sip;
    unsigned long dip;
    unsigned int protocol;    
} filter;

class rawsocsniffer:public rawsocket
{
    private:
	filter simfilter;
	char *packet;
	static const int max_packet_len = 2048;
    public:
	rawsocsniffer(int protocol):rawsocket(protocol){}
	// ~rawsocsniffer(){ return }
	bool init();
	bool testbit(const unsigned int p, int k);
	void setbit(unsigned int &p,int k);
	void analyze();
	void ParseRARPPacket();
	void ParseARPPacket();
	void ParseIPPacket();
	void ParseTCPPacket();
	void ParseUDPPacket();
	void ParseICMPPacket();
	void print_hw_addr(const unsigned char *ptr);
	void print_ip_addr(const unsigned int ip);
public:
	void sniffer();
	void setfilter(filter myfilter);
};